package com.feeyin.radar.crm.dao.impl;

import com.feeyin.radar.commons.DateUtils;
import com.feeyin.radar.commons.Page;
import com.feeyin.radar.commons.StringUtils;
import com.feeyin.radar.crm.dao.SaleCostDAO;
import com.feeyin.radar.crm.model.SaleCost;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

public class SaleCostDAOImpl extends SqlMapClientDaoSupport
  implements SaleCostDAO
{
  public List getPageSaleCosts(SaleCost saleCost, Page page)
  {
    if (saleCost == null) return null;
    if (page == null) return null;
    saleCost.setPageStartRow(page.getPageStartRow());
    saleCost.setPageEndRow(page.getPageEndRow());
    return getSqlMapClientTemplate().queryForList("getPageSaleCostsByDynamic", saleCost);
  }

  public Integer getPageSaleCostsCount(SaleCost saleCost) {
    if (saleCost == null) return Integer.valueOf(0);
    return (Integer)getSqlMapClientTemplate().queryForObject("getPageSaleCostsCountByDynamic", saleCost);
  }

  public List getSaleCostsBySaleCostCode(String saleCostCode) {
    if (StringUtils.isEmpty(saleCostCode)) return null;
    SaleCost saleCost = new SaleCost();
    saleCost.setSaleCostCode(saleCostCode);
    return getSqlMapClientTemplate().queryForList("getSaleCostsByDynamic", saleCost);
  }

  public BigDecimal getTotalSaleCostBySaleCostCode(String saleCostCode) {
    if (StringUtils.isEmpty(saleCostCode)) return null;
    SaleCost saleCost = new SaleCost();
    saleCost.setSaleCostCode(saleCostCode);
    return (BigDecimal)getSqlMapClientTemplate().queryForObject("getTotalSaleCostByDynamic", saleCost);
  }

  public BigDecimal getTotalSaleCostByDynamic(SaleCost saleCost) {
    if (saleCost == null) return null;
    return (BigDecimal)getSqlMapClientTemplate().queryForObject("getTotalSaleCostByDynamic", saleCost);
  }

  public BigDecimal getPeriodSaleCost(Integer currentUserId, String assignPeriod)
  {
    if (currentUserId == null) return null;
    if (StringUtils.isEmpty(assignPeriod)) return null;

    Date startDate = DateUtils.getCurrentYearFirstDay();
    Date endDate = DateUtils.getCurrentYearFirstDay();

    if ("1".equals(assignPeriod)) {
      startDate = DateUtils.getCurrentMonthFirstDay();
      endDate = DateUtils.getCurrentMonthEndDay();
    } else if ("2".equals(assignPeriod)) {
      startDate = DateUtils.getCurrentSeasonFirstDay();
      endDate = DateUtils.getCurrentSeasonEndDay();
    } else if ("3".equals(assignPeriod)) {
      startDate = DateUtils.getCurrentYearFirstDay();
      endDate = DateUtils.getCurrentYearFirstDay();
    }

    SaleCost saleCost = new SaleCost();
    saleCost.setApplyPersonId(currentUserId);
    saleCost.setStatus("2");
    saleCost.setStartDate(startDate);
    saleCost.setEndDate(endDate);
    return getTotalSaleCostByDynamic(saleCost);
  }

  public boolean insertSaleCost(SaleCost saleCost) {
    if (saleCost == null) return false;
    Integer result = (Integer)getSqlMapClientTemplate().insert("insertSaleCost", saleCost);
    return result != null;
  }

  public boolean updateSaleCostBySaleCostCode(SaleCost saleCost)
  {
    if (saleCost == null) return false;
    if (StringUtils.isEmpty(saleCost.getSaleCostCode())) return false;
    Integer result = Integer.valueOf(getSqlMapClientTemplate().update("updateSaleCostByDynamic", saleCost));
    return result != null;
  }

  public boolean deleteSaleCost(String saleCostCode)
  {
    if (StringUtils.isEmpty(saleCostCode)) return false;
    Integer result = Integer.valueOf(getSqlMapClientTemplate().delete("deleteSaleCost", saleCostCode));
    return result != null;
  }
}